// pages/expert/expertDetail.js

const defaultSettings = require('../../config/defaultSettings')
import {repair,save} from '../../api/eqRepair'
import {eqUrl,queryEqByEqNo} from '../../api/eq'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultSettings,
    repairDetail:{eqNo:'',mapLongitude:'',mapLatitude:'',eqLocal:'',operatorName:'',isRepair:'0',empPhone:app.globalData.appInfo.emp.empPhone},
    repairDetailForm:null,
    eqNo:null,
    primaryColor:defaultSettings.primaryColor,
    emp:app.globalData.appInfo.emp,
    loading:false,
    isShowCondition:false,
    formAttributes:[
      {field:'eqName',label:'设备名称',component:'input',readonly:false,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: true, message: '不能为空'}]},
      {field:'eqType',label:'设备类型',component:'select',readonly:false,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: true, message: '不能为空'}],dictCode:defaultSettings.dictCodes.eqFenlei},
      // {field:'eqNum',label:'设备数量',component:'input',readonly:false,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: true, message: '不能为空'}],type:'number'},
      {field:'pwNo',label:'所属机井',component:'cascader',readonly:false,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: true, message: '不能为空'}],dictCode:defaultSettings.dictCodes.empPumpTree,dictParam:app.globalData.appInfo.emp.empNo,finish:true},
      
      {field:'eqNo',label:'设备编号',component:'input',readonly:true,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: false, message: '不能为空'}],buttonText:"扫码"},
      {field:'operatorName',label:'报修人',component:'input',readonly:true,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: false, message: '不能为空'}]},
      {field:'empPhone',label:'联系电话',component:'input',readonly:false,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: true, message: '不能为空'}]},

      {field:'repairDesc',label:'故障原因',component:'select',readonly:false,required:true,isShowLabel:true,border:true,clean:true,dictCode:defaultSettings.dictCodes.gzyy,rules: [{required: true, message: '不能为空'}]},

      {field:'eqLocal',label:'设备位置',component:'input',type:'textarea',readonly:true,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: true, message: '不能为空'}]},

      {field:'mapLongitude',label:'经度',component:'input',readonly:true,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: true, message: '不能为空'}]},

      {field:'mapLatitude',label:'纬度',component:'input',readonly:true,required:true,isShowLabel:true,border:true,clean:true,rules: [{required: true, message: '不能为空'}]},

    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: "设备报修",
    })
    wx.removeStorageSync('repairDetail');
    this.setData ({'repairDetail.operatorName':this.data.emp.empName})
    this.data.repairDetailForm = this.selectComponent("#repairDetailForm")
    
    // let repairDetail = wx.getStorageSync('eqRepairItem')
    // this.setData({repairDetail:repairDetail})

    // if(this.data.repairDetail.isRepair===defaultSettings.constants.no && this.data.emp.empType===defaultSettings.constants.weixiugong){
    //   const repairContentAtt = `formAttributes[4].hide`;
    //   this.setData({[repairContentAtt]:false})
    // }
  },
  onChange(e){},
  openPopup(){
    this.setData({isShowPopup:true})
  },
  onClose(){
    this.setData({isShowPopup:false})
  },
  enterEqNo(e){
    this.setData({eqNo:e.detail})
  },
  onQueryEq(){
    if(!this.data.eqNo){
      wx.showToast({icon:"none",title: '请输入设备编号'})
      return false
    }

    queryEqByEqNo({eqNo:this.data.eqNo}).then(res=>{
      if(res.success){
        res.result.keyId=null
        this.setData({repairDetail:Object.assign(this.data.repairDetail, res.result)})
      }else{
        wx.showToast({icon:"none",title: res.message})
        this.setData({'repairDetail.eqNo':''})
      }
    })

    this.onClose()
  },
  onClick(e){
    if(e.detail.field==='eqNo'){
      wx.scanCode({
        onlyFromCamera: false,
        scanType: [],
        success: (r) => {
          //调用后台根据编号查询
          let eqNo = r.result
          if (eqNo && eqNo.indexOf(":") >= 0) {
            eqNo = eqNo.substring(eqNo.indexOf(":") + 1)
          }
          queryEqByEqNo({eqNo:eqNo}).then(res=>{
            if(res.success){
              res.result.keyId=null
              this.setData({repairDetail:Object.assign(this.data.repairDetail, res.result)})
              // this.setData({'repairDetail.eqNo':eqNo})
            }else{
              wx.showToast({icon:"none",title: res.message})
              this.setData({'repairDetail.eqNo':''})
            }
          })
        },
        fail: (res) => {
          wx.showToast({icon:"none",title: "扫码失败"})
        },
        complete: (res) => {},
      })
    }
  },
  getLocation(){
    let that = this
    // wx.getLocation({
    //   type: 'gcj02',
    //   success (res) {
    //     console.log(res)
    //     const latitude = res.latitude
    //     const longitude = res.longitude
     
    //     that.setData({'repairDetail.mapLongitude':res.longitude,"repairDetail.mapLatitude":res.latitude})
    //   }
    //  })

    wx.chooseLocation({
      success:(r)=>{
        let repairDetail = wx.getStorageSync('repairDetail')
        if(repairDetail){
          this.setData({repairDetail:repairDetail})
        }
        that.setData({'repairDetail.mapLongitude':r.longitude,"repairDetail.mapLatitude":r.latitude,"repairDetail.eqLocal":r.address})
      },
      fail: (err) => {

      }
    })
  },
  onSubmit(){
    if(this.data.repairDetailForm.onVerify()){
      this.setData({loading:true})
      this.data.repairDetail = this.data.repairDetailForm.getFormData()
      save(this.data.repairDetail).then(res=>{
        if(res.success){
          wx.showToast({title: '操作成功',icon: 'success'})
          this.setData({'repairDetail.keyId':res.result.keyId})
        }else{
          wx.showToast({icon:"none",title: res.message})
        }
      }).finally(()=>{
        this.setData({loading:false})
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    wx.setStorageSync('repairDetail', this.data.repairDetailForm.getFormData());
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})